package com.mobileares.midp.widgets.client.tree;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.mobileares.midp.widgets.client.panel.EventFlowPanel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-3-22
 * Time: 15:44:09
 * To change this template use File | Settings | File Templates.
 */
public class CTreeItem extends Composite {
    private CTree.ImageAdapter images = new CTree.ImageAdapter();
    public EventFlowPanel item;
    private InlineLabel textWidget;
    private CTree tree;
    private Image leafImage;
    private Image openImage;
    private Image closeImage;
    private List<CTreeItem> children;
    private int level = 0;
    private boolean isFire = false;
    private Object userObject;

    private CTreeItem parentItem;

    public CTreeItem getParentItem() {
        return parentItem;
    }

    public void setParentItem(CTreeItem parentItem) {
        this.parentItem = parentItem;
    }

    public CTreeItem(String text) {
        textWidget = new InlineLabel(text);
        textWidget.setStyleName("text");
        leafImage = images.treeLeaf().createImage();

        item = new EventFlowPanel();
        SimplePanel imageWrapper = new SimplePanel();
        imageWrapper.setStyleName("t-image");
        imageWrapper.add(leafImage);
        item.add(imageWrapper);
        item.add(textWidget);
        item.setStyleName("item");
        initWidget(item);
        addEvent();
    }

    public String getText() {
        return textWidget.getText();
    }

    public Object getUserObject() {
        return userObject;
    }

    public void setUserObject(Object userObject) {
        this.userObject = userObject;
    }

    void setLevel(int level) {
        this.level = level;
        if (children != null) {
            for (CTreeItem item : children)
                item.setLevel(level + 1);
        }
    }


    void setTree(CTree tree) {
        this.tree = tree;
    }

    public void addTreeItem(CTreeItem item) {
        if (children == null) {
            children = new ArrayList<CTreeItem>();
            afterAdd(item);
        }
        children.add(item);
        item.setLevel(level + 1);
        item.setParentItem(this);
    }

    private void afterAdd(CTreeItem child) {
        openImage = images.treeOpen().createImage();
        closeImage = images.treeClosed().createImage();
        closeImage.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                //todo:open;
                open();

            }
        });
        openImage.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                //todo:close
                close();
            }
        });
        replaceTreeNodeImage(closeImage);
    }

    private void replaceTreeNodeImage(Image image) {
        ((SimplePanel) (item.getWidget(0))).setWidget(image);
//        item.insert(image, 0);
    }

    void open() {
        if (getChildren() != null) {
            tree.openTreeItem(this);
            replaceTreeNodeImage(openImage);
        }

    }

    void close() {
        if (children == null) return;
        tree.closeTreeItem(CTreeItem.this);
        replaceTreeNodeImage(closeImage);
    }

    public List<CTreeItem> getChildren() {
        return children;
    }

    boolean isFire() {
        return isFire;
    }

    void setFire(boolean fire) {
        isFire = fire;
    }

    void setPadding() {
        DOM.setStyleAttribute(getElement(), "paddingLeft", 15 * level + "px");
    }

    private void addEvent() {
        item.addMouseOverHandler(new MouseOverHandler() {
            public void onMouseOver(MouseOverEvent event) {
                CTreeItem selected = tree.getSelected();
                if (selected != null && selected == CTreeItem.this) return;
                item.addStyleName("over");
            }
        });
        item.addMouseOutHandler(new MouseOutHandler() {
            public void onMouseOut(MouseOutEvent event) {
                item.removeStyleName("over");
            }
        });
        item.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                CTreeItem old = tree.getSelected();
                if (old != null) old.removeStyleName("selected");
                item.addStyleName("selected");
                tree.setSelected(CTreeItem.this);
                SelectionEvent.fire(tree, CTreeItem.this);
            }
        });
    }

}
